class ApoController < ApplicationController
  #caches_page :home

  def login
      if request.post?
        @member = Member.authenticate(params[:username], params[:password])
        if @member.nil?
          flash[:warning] = "Invalid username or password"
          redirect_to :action => "login"
        elsif @member.deleted?
          flash[:warning] = "You do not have permission to login. Please contact membership@apouiuc.com"
          redirect_to :action => "login"
        else
          session[:user] = @member
          redirect_to :controller => "apo", :action => "home"
        end
      end
  end

  def home
    @member = Member.find_by_id(session[:user])
    @memberData = Member.getData(@member, Event.getCurrentSemester)
    @srcToggle = Toggle.find_by_name("SRC").enabled
		@events = @member.events.find_all_by_semester_index(Event.getCurrentSemester,
			:conditions => "events.event_type <> 'Dues' AND events.event_type <> 'Dues (late)' AND events.event_type <> 'Committee' AND events.deleted <> 1",
			:order => "date DESC")
		@birthdays = Member.find(:all, :select => "id, first_name, last_name, birthday, (YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5)) AS age",
                  					:order => "MONTH(birthday), DAY(birthday), last_name, first_name",
                  					:conditions => "(DAY(birthday) >= DAY(CURDATE()) AND DAY(birthday)-DAY(CURDATE()) <= 15 AND MONTH(birthday) = MONTH(CURDATE())) OR
              									(DAY(CURDATE())-DAY(birthday) >= 15 AND MONTH(birthday) = 1 AND MONTH(CURDATE()) = 12) OR
              									(DAY(CURDATE())-DAY(birthday) >= 15 AND MONTH(birthday) - MONTH(CURDATE()) = 1)")
		@announcements = Announcement.find_all_by_admin(true, :order => "created_at DESC", :limit => 15)
		@shoutouts = Announcement.find_all_by_admin(false, :order => "created_at DESC", :limit => 15)
  end

  def logout
    reset_session
    redirect_to :action => "login"
  end

  def reset_password
    @member = Member.find_by_username(params[:netid])
    if @member.nil?
      flash[:warning] = "No such NetID exists!"
      render :action => "reset"
    else
      @newPassword = (0...8).map{ ('a'..'z').to_a[rand(26)] }.join
      @member.password = Member.hash_password(@newPassword)
      @member.save
      if @member.email.nil? or @member.email == ""
        @email = @member.username + "@illinois.edu"
      else
        @email = @member.email
      end
      flash[:notice] = "Your new password has been sent to #{@email}."
      Emailer.deliver_password(@newPassword, @email, @member.first_name)
      redirect_to :action => "reset"
    end
  end

  def download_application
    #locally
    #send_file '../files/umoc.doc', :type => 'application/msword'

    #remotely
    send_file "#{RAILS_ROOT}/public/files/umoc.doc", :type => 'application/msword', :disposition => 'attachment'

    #redirect_to :action => "umoc"
  end

  def name_select_show
      names = params[:member][:name].split(' ')
      if names[2] then names[1] = names[1] + " " + names[2] end
      @member = Member.find_by_first_name_and_last_name_and_deleted(names[0], names[1], false)
      if !@member.nil?
        @memberData = Member.getData(@member, Event.getCurrentSemester)
        redirect_to :controller => "members", :action => "show", :id => @member
      else
        flash[:error] = "Member: #{params[:member][:name]} not found!"
        @member = session[:user]
        redirect_to :back
      end
  end

  def auto_complete_for_member_name
    search = params[:member][:name].gsub("'", "\\\\'");
    @members = Member.find(:all, :conditions => "deleted = FALSE AND (first_name LIKE '#{search}%' OR last_name LIKE '#{search}%')") unless search.blank?
    render :partial => "members/search"
  end

  def post_admin
    if Member.find(params[:member_id]).admin?
      announcement = Announcement.new
      announcement.admin = true
      announcement.message = params[:message]
      announcement.member_id = params[:member_id]
      announcement.save
    else
      flash[:error] = "You are not allowed to do that!"
    end

    redirect_to :action => "home"
  end

  def post_member
    announcement = Announcement.new
    announcement.admin = false
    announcement.message = params[:message]
    announcement.member_id = params[:member_id]
    announcement.save

    redirect_to :action => "home"
  end

  def delete_post
    announcement = Announcement.find(params[:id])
    announcement.destroy

    redirect_to :action => "home"
  end

end
